#define _CRT_SECURE_NO_WARNINGS 1
#include "seqlist.h"

#if 0
void SLprint(SL* pf)
{
	for (int i = 0; i < pf->size; i++)
	{
		printf("%d ", pf->arr[i]);
	}
}

void SLinit(SL* pf)
{
	pf->arr = NULL;
	pf->count = pf->size = 0;
}

void SLdate(SL* pf)
{
	if (pf->arr)
	{
		pf = NULL;
	}
	pf->count = pf->size = 0;
}

void SLcount(SL* pf)
{
	if (pf->count == pf->size)
	{
		int ret = pf->count == 0 ? 4 : pf->count * 2;
		SLtype* pf1 = (SLtype*)realloc(pf,ret*sizeof(SLtype));
		assert(pf1);
		pf->count = ret;
		pf->arr = pf1;
	}
}

void SLpushback(SL* pf, SLtype x)
{
	SLcount(pf);
	pf->arr[pf->size] = x;
	pf->size++;
}

void SLpushfront(SL* pf, SLtype x)
{
	SLcount(pf);
	int i = 0;
	for (i = pf->size - 1; i > 0; i--)
	{
		pf->arr[i + 1] = pf->arr[i];
	}
	pf->size++;
}

void SLbackdate(SL* pf)
{
	assert(pf);
	assert(pf->size);
	pf->size--;
}

void SLforntdate(SL* pf)
{
	assert(pf);
	assert(pf->size);
	int i = 0;
	for (i = 1; i < pf->size; i++)
	{
		pf->arr[i - 1] = pf->arr[i];
	}
}
#endif

//

#if 0
void SLinit(SL* pf)
{
	pf->arr = NULL;
	pf->size = pf->SLmemory = 0;
}

void SLdate(SL* pf)
{
	if (pf->arr)
	{
		free(pf);
	}
	pf->arr = NULL;
	pf->size = pf->SLmemory = 0;
}

int  SlMemory(SL* pf)
{
	if (pf->size == pf->SLmemory)
	{
		int newSLmemory = pf->SLmemory == 0 ? 4 : 2 * pf->SLmemory;
		SLtype* pf1 = (SLtype*)realloc(pf->arr, newSLmemory * sizeof(SLtype));
		if (pf1 == NULL)
		{
			perror("realloc:pf1");
			return 1;
		}
		pf->arr = pf1;
		pf->SLmemory = newSLmemory;
	}
	return 0;
}

void SLpushback(SL* pf, SLtype x)
{
	SlMemory(pf);
	pf->arr[pf->size] = x;
	pf->size++;
}

void SLpushfront(SL* pf, SLtype x)
{
	SlMemory(pf);
	int i;
	for (i = pf->size; i > 0; i--)
	{
		pf->arr[i] = pf->arr[i-1];
	}
	pf->arr[0] = x;
	pf->size++;
}

void SLbackdate(SL* pf)
{
	assert(pf);
	assert(pf->arr);
	pf->size--;
}

void SLfrontdate(SL* pf)
{
	assert(pf);
	assert(pf->arr);
	int i;
	for (i = 1; i<pf->size; i++)
	{
		pf->arr[i - 1] = pf->arr[i];
	}
	pf->size--;
}
#endif

#if 1

void SLinit(SL* pf)
{
	pf->arr = NULL;
	pf->size = pf->SLmemory = 0;
}

void SLdate(SL* pf)
{
	if (pf->arr)
	{
		free(pf->arr);
	}
	pf->arr = NULL;
	pf->size = pf->SLmemory = 0;
}

void SlMemory(SL* pf)
{
	if (pf->size == pf->SLmemory)
	{
		int newSLmemory = pf->SLmemory == 0 ? 4 : 2 * pf->SLmemory;
		SLtype* pf1 = realloc(pf->arr, newSLmemory * sizeof(SLtype));
		if (pf1 == NULL)
		{
			perror("realloc:pf1 faild");
			exit(1);
		}
		pf->arr = pf1;
		pf->SLmemory = newSLmemory;
	}
}

void SLpushback(SL* pf, SLtype x)
{
	SlMemory(pf);
	pf->arr[pf->size] = x;
	pf->size++;
}

void SLpushfront(SL* pf, SLtype x)
{
	SlMemory(pf);
	int i;
	for (i = pf->size; i>0; i--)
	{
		pf->arr[i] = pf->arr[i-1];
	}
	pf->arr[0] = x;
	pf->size++;
}

void SLbackdate(SL* pf)
{
	assert(pf);
	assert(pf->arr);
	pf->size--;
}

void SLforintdate(SL* pf)
{
	assert(pf);
	assert(pf->arr);
	int i;
	for (i = 0; i < pf->size-1; i++)
	{
		pf->arr[i] = pf->arr[i + 1];
	}
	pf->size--;
}

void SLprintf(SL*pf)
{
	int i = 0;
	for (i = 0; i < pf->size; i++)
	{
		fprintf(stdout, "%d ", pf->arr[i]);
	}
	fprintf(stdout, "\n");
}

#endif